import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import path from "path";
import CopyPlugin from "vite-plugin-files-copy";
import topLevelAwait from "vite-plugin-top-level-await";


export default defineConfig(({ mode }) => {
  const isProduction = mode === "production";

  return {
    base: "/html/document/",
    // base: "./",
    plugins: [
      vue(),
      AutoImport({
        dts: "src/types/auto-imports.d.ts",
        imports: ["vue", "vue-router", "pinia"],
        eslintrc: {
          enabled: false,
          filepath: "./.eslintrc-auto-import.json",
          globalsPropValue: true,
        },
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        dirs: ["src/components"],
        resolvers: [ElementPlusResolver()],
      }),
      CopyPlugin({
        patterns: [
          {
            from: "./public/ribbon.xml",
            to: "./dist/ribbon.xml",
          },
        ],
      }),
      topLevelAwait({promiseExportName: '__tla', promiseImportName: (i) => `__tla_${i}`})
    ],
    server: {
      host: "0.0.0.0",
      port: 18888,
      cors: true,
      open: true,
      proxy: {
        "/unify-auth": {
            target: "https://iam-api-qa.qdama.cn",
            // target: "http://localhost:9023",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/unify-auth/, "/unify-auth"),
        },
      },
    },
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
      },
      extensions: [".js", ".ts", ".json", ".tsx", ".vue"],
    },
    build: {
      outDir: `dist-${mode}`,
      assetsDir: "./static",
      chunkSizeWarningLimit: 4096,
      assetsInlineLimit: 4096,
      minify: isProduction ? "terser" : false,
      manifest: false,
      sourcemap: false,
      terserOptions: {
        compress: {
          drop_console: isProduction,
          drop_debugger: isProduction,
        },
      },
      rollupOptions: {
        output: {
          chunkFileNames: "static/js/[name]-[hash].js",
          entryFileNames: "static/js/[name]-[hash].js",
          assetFileNames: "static/[ext]/[name]-[hash].[ext]",
          manualChunks: {
            core: ["vue", "vue-router"],
          },
        },
      },
    },
    define: {
      __VUE_PROD_DEVTOOLS__: isProduction ? "false" : "true",
      __VUE_OPTIONS_API__: true,
      __VUE_I18N_FULL_INSTALL__: true,
      __INTLIFY_PROD_DEVTOOLS__: isProduction ? "false" : "true",
      // 添加其他环境变量
    },
  };
});
